var deptDatagrid = null;
//定义全局部门ID ARR
var deptid = null;
var deptidArr = null;
//插入or修改标志:0,新增 1,修改
var upsert = 0;

$(function() {
	initDialog();
	initMsgDatagird();
	//var width = $("div .flexigrid").width();
	//var height = $("#leftDIV_z").height();
	//MZ.showMsg(height);
	$("#addDeptA").bind("click", function(){
		upsert = 0;
		$("#ui-dialog-title-deptDialog").text("新增部门");
		MZ.openDialog("deptDialog");
	});
	
	$("#updateDeptA").bind("click", function(){
		upsert = 1;
		deptid = null;
		MZ.clearForm("deptForm");
		var rowVals = deptDatagrid.getRowValues();
		//reset
		if(rowVals.length == 1){
			rowVals = rowVals[0];
			deptid = rowVals.id;
			$("#deptname").val(rowVals.name);
			$("#deptdesc").val(rowVals.description);
			$("#ui-dialog-title-deptDialog").text("修改部门");
			MZ.openDialog("deptDialog");
		}else{
			MZ.showMsg("请选择一行记录修改!");
		}
		
	});
	
	$("#delDeptA").bind("click", function(){
		deptidArr = null;
		deptidArr = deptDatagrid.getRowValue(2);
		if(deptidArr.length >= 1){
			MZ.openDialog("dialog-confirm");
		}else{
			MZ.showMsg("请选择一行记录修改!");
		}
	});
	
	$("#submitBtn").bind("click", function(){
		if(upsert == 0){
			addDept();
		}else if(upsert == 1){
			updateDept();
		}
		
	});
	$("#resetBtn").bind("click", function(){
		MZ.clearForm("deptForm");
	});
	
	$("#searchA").bind("click", function(){
		searchDept();
	});
	
	$("#resetA").bind("click", function(){
		$("#deptNameText").val("");
	});
	
	//VALIDATION FORM//
	var validator = $("#deptForm").validate({ 
		debug: true,
		rules: { 
		    deptname: {
		        required: true, 
		        maxlength: 32
		    }
		}, 
		messages: { 
			deptname: "请输入部门名称!"
		}, 
		errorPlacement: function(error, element) { 
		    error.appendTo( element.prev() ); 
		}, 
		submitHandler: function() { 
		    MZ.showMsg("Validate!"); 
		    return false;
		}, 
		success: function(label) { 
		    label.html("&nbsp;").addClass("valid_small"); 
		} 
	});
});

function initDialog(){
	//DIALOG//
	$('#deptDialog').dialog({
		autoOpen: false,
		width: 500,
		height: 400,
		modal: true
	});
	
	$( "#dialog-confirm" ).dialog({
		resizable: false,
		autoOpen: false,
		height:180,
		modal: true,
		buttons: {
			"确认删除": function() {
				delDept();
				$( this ).dialog( "close" );
			},
			"取消": function() {
				$( this ).dialog( "close" );
			}
		}
	});
}

//init message datagird
//get json data from server
function initMsgDatagird(){
	//var width = $("#searchBox").width()-0;
	deptDatagrid = $("#msgDatagird").flexigrid({
		url: "branchmanager/getDeptInfo",
		colModel : [
			{display: "ID", name : "id", width : 90, sortable : true, align: "center", hide: true},
			{display: "部门名称", name : "name", width : 150, sortable : true, align: "center"},
			{display: "创建人", name : "creator", width : 120, sortable : true, align: "center", hide: false},
			{display: "创建时间", name : "gmtCreate", width : 150, sortable : true, align: "center", hide: false},
			{display: "备注", name : "description", width : 400, sortable : true, align: "left"}
		]
    });
}

//funciton onSelectRow required
//update flexigrid onselectrow function
//if you need't use onselect row
//let this function blank to do nothing
function onSelectRow(tr){
	
}

function searchDept(){
	var deptname = $("#deptNameText").val();
	var params = [{name:"deptname", value: deptname}];
	$("#msgDatagird").flexOptions({newp: 1, params: params}).flexReload();
}

function addDept(){
	var params = $("#deptForm").formSerialize();
	var deptName = $("#deptname").val();
	if(!checkDept(deptName)) {
		return false;
	}
	$.ajax({
		url : "branchmanager/addDeptInfo",
		// processData: false,
		type : "post",
		data : params,
		beforeSend : function() {
			MZ.clearForm("deptForm");
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
			MZ.closeDialog("deptDialog");
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

function updateDept(){
	var params = $("#deptForm").formSerialize()+"&deptid="+deptid;
	var deptName = $("#deptname").val();
	if(!checkDept(deptName)) {
		return false;
	}
	$.ajax({
		url : "branchmanager/updateDeptInfo",
		// processData: false,
		type : "post",
		data : params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
			MZ.closeDialog("deptDialog");
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

function delDept(){
	var params = {"ids":deptidArr.toString()};
	$.ajax({
		url : "branchmanager/delDeptInfo",
		// processData: false,
		type : "post",
		data : params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

//VALID FORM
function checkDept(deptName){
	if($.trim(deptName)==""){
		MZ.showMsg("部门名称不能为空!");
		return false;
	}
	return true;
}
